<template>
    <div>昵称：{{$store.state.user.nickname}}
        <div>手机号：<input type="text" placeholder="请输入手机号" v-model="cellphone"></div>
        <div>密码：<input type="password" placeholder="请输入密码" v-model="password"></div>
        <div><button type="button" @click="submit()">登录</button></div>
    </div>
</template>

<script>
    import {mapActions,mapMutations} from "vuex";
  export default {
    name: 'login',
    data(){
      return {
        cellphone:"",
        password:""
      }
    },
    methods:{
      ...mapActions({
        doLogin:"user/doLogin"
      }),
      ...mapMutations({
        SET_USER_LOGIN:"user/SET_USER_LOGIN"
      }),
      async submit() {
        if(this.cellphone.trim()==""){
          alert("请输入用户名");
          return;
        }
        if(this.password.trim()==''){
          alert("请输入密码");
          return;
        }
        let loginRes=await this.doLogin({cellphone:this.cellphone,password:this.password});
        // console.log(loginRes);
        if(loginRes.code==200){
          this.SET_USER_LOGIN({nickname:loginRes.data.nickname,uid:loginRes.data.uid,isLogin:true,authToken:loginRes.data.auth_token});
            this.$router.replace("/");
        }else{
          alert(loginRes.data);
        }
        // this.$request({
        //   url:"/home/user/pwdlogin?token="+this.$config.token,
        //   method:"post",
        //   data:{cellphone:this.username,password:this.password}
        // }).then(res=>{
        //   // console.log(res);
        //   localStorage['username']=res.data.nickname;
        //   localStorage['uid']=res.data.uid;
        //   localStorage['authToken']=res.data.auth_token;
        //   localStorage['isLogin']=true;
        //   this.$router.replace("/");
        // })
      }
    }
  };
</script>

<style scoped>

</style>
